package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.User;
import util.SocketUtil;
import util.StateCode;

public class LoginPanel extends JPanel implements ActionListener, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] jtf = new JTextField[2];
	private JButton[] jb = new JButton[2];
	private boolean isrequest = true;
	private User user;

	public LoginPanel() {
		setOpaque(false);
		setLayout(new GridBagLayout());
		Font font = new Font("微软雅黑", Font.LAYOUT_RIGHT_TO_LEFT, 20);
		String[] s1 = { "昵称", "对方IP" };
		String[] s2 = { "登录", "关闭" };
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 30, 10, 0);
		for (int i = 0; i < s1.length; i++) {

			gbc.anchor = GridBagConstraints.CENTER;
			JLabel jl = new JLabel(s1[i]);
			jl.setFont(font);
			gbc.gridx = 0;
			gbc.gridy = i;
			add(jl, gbc);
			jtf[i] = new JTextField(10);
			jtf[i].setFont(font);
			gbc.gridx = 1;
			add(jtf[i], gbc);
			jb[i] = new JButton(s2[i]);
			jb[i].setFont(font);
			jb[i].addActionListener(this);
			gbc.gridx = i;
			gbc.gridy = 2;
			add(jb[i], gbc);

		}

	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite composite = g2.getComposite();
		g2.setColor(Color.gray);
		g2.setComposite(AlphaComposite.SrcOver.derive(0.9f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(composite);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jb[0]) {
			String name = jtf[0].getText();
			String ip = jtf[1].getText();
			if (name.equals("") || ip.equals("")) {
				JOptionPane.showMessageDialog(this, "请输入昵称对方IP！");
				return;
			}
			user=SocketUtil.getUser();user.setName(name);user.setIp(ip);
			if (isrequest) {

				try {
					Socket socket = new Socket(ip, 6666);
					SocketUtil.setSocket(socket);
					SocketUtil.SendMessage(StateCode.REQUESTLINK);

				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			else
			{
				this.setVisible(false);
				SocketUtil.SendMessage(user);
			}
			
		}
		else
		{
			System.exit(0);
		}

	}

	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof Integer)
		{
			int code=(int) arg;
			switch (code) {
			case StateCode.REQUESTLINK:
				int f=JOptionPane.showConfirmDialog(this, "对方请求想和你进行游戏是否同意", "提示信息", JOptionPane.YES_NO_OPTION);
				if(f==0)
				{
					SocketUtil.SendMessage(StateCode.AGREEREQUEST);
					isrequest=false;
					jtf[1].setText(SocketUtil.getSocket().getInetAddress().getHostAddress());
					jtf[1].setEnabled(false);
				}
				else
				{
					SocketUtil.SendMessage(StateCode.DISAGREEREQUEST);
				}
				break;
			case StateCode.AGREEREQUEST:
				this.setVisible(false);
				SocketUtil.SendMessage(user);
				break;
			case StateCode.DISAGREEREQUEST:
				JOptionPane.showMessageDialog(this, "对方拒绝了请求");
				break;
			case StateCode.LINKREST:
				SocketUtil.StartServer();
				this.setVisible(true);
				break;
			}
		}

	}

}
